package com.example.campuslink.commen.dialog

import android.content.Context
import android.media.MediaPlayer
import android.net.Uri
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.example.campuslink.R
import com.example.campuslink.commen.core.CoreDialog
import com.example.campuslink.databinding.DialogShowVideoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File

class ShowVideoDialog : CoreDialog<DialogShowVideoBinding>(R.layout.dialog_show_video) {
    private var uri: Uri? = null
    private lateinit var context: Context
    var videoPath = ""
    private var status = "start"
    override fun onAttach(context: Context) {
        super.onAttach(context)
        this.context = context
    }

    private fun performFileDeletion(filePath: String) {
        lifecycleScope.launch(Dispatchers.IO) {
            val file = File(filePath)
            val result = if (file.exists() && file.isFile) {
                file.delete()
            } else {
                false
            }

            withContext(Dispatchers.Main) {
                if (result) {
                    Toast.makeText(requireContext(), "删除成功", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(requireContext(), "删除失败", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    override fun setupViews() {
        uri = Uri.parse(videoPath)
        binding.videoView.setVideoURI(uri)

        onClick()
    }

    fun onClick() {
        binding.btnCancel.setOnClickListener({
            performFileDeletion(videoPath)
            dismiss()
        })
        binding.btnConfirm.setOnClickListener({
            Toast.makeText(requireContext(), "保存成功", Toast.LENGTH_SHORT)
            dismiss()
        })
        binding.ivStop.setOnClickListener({
            if (status == "start") {
                status = "stop"
                Glide.with(requireContext()).load(R.drawable.start).into(binding.ivStop)
                binding.videoView.pause()
                binding.ivStop.setVisibility(View.GONE)
            } else if (status == "stop") {
                status = "start"
                Glide.with(requireContext()).load(R.drawable.stop).into(binding.ivStop)
                binding.videoView.start()
                binding.ivStop.setVisibility(View.GONE)
            }
        })
        binding.videoView.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
                if (p1?.action == MotionEvent.ACTION_DOWN) {
                    binding.ivStop.setVisibility(View.VISIBLE)
                }
                return false
            }
        })
        binding.videoView.setOnPreparedListener(object : MediaPlayer.OnPreparedListener {
            override fun onPrepared(p0: MediaPlayer?) {
                binding.videoView.start()
            }
        });
        binding.videoView.setOnCompletionListener(object : MediaPlayer.OnCompletionListener {
            override fun onCompletion(p0: MediaPlayer?) {
                binding.videoView.start()
            }
        });
    }

    override fun createBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): DialogShowVideoBinding {
        return DialogShowVideoBinding.inflate(inflater, container, false)
    }
}